package com.xiaoshuidi.cloud.module.rooms.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.rooms.enums.RentStatusEnum;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomStateRecordMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomStateRecord;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author hans.zhang
 */
@Component
@RequiredArgsConstructor
public class RoomStateRecordRepository extends ServiceImpl<RoomStateRecordMapper, RoomStateRecord> {
    /**
     * 获取房源下的当前房态
     *
     * @param roomHouseId
     * @return
     */
    public RoomStateRecord getByRoomIdAndIsCurrentState(@NonNull Long roomHouseId) {
        LambdaQueryWrapper<RoomStateRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomStateRecord::getRoomId, roomHouseId);
        wrapper.eq(RoomStateRecord::getIsCurrentState, true);
        return this.getOne(wrapper);
    }

    public List<RoomStateRecord> getStatesByRoomIdAndIsCurrentState(@NonNull Long roomHouseId) {
        LambdaQueryWrapper<RoomStateRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomStateRecord::getRoomId, roomHouseId);
        wrapper.eq(RoomStateRecord::getIsCurrentState, true);
        return this.list(wrapper);
    }

    public List<RoomStateRecord> listByRoomIdAndIsCurrentState(@NonNull Long roomHouseId) {
        LambdaQueryWrapper<RoomStateRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomStateRecord::getRoomId, roomHouseId);
        wrapper.eq(RoomStateRecord::getIsCurrentState, true).orderByDesc(RoomStateRecord::getId);
        return this.list(wrapper);
    }

    public List<RoomStateRecord> getListByRoomIdAndIsCurrentState(List<Long> ids) {
        LambdaQueryWrapper<RoomStateRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.in(RoomStateRecord::getRoomId, ids);
        wrapper.eq(RoomStateRecord::getIsCurrentState, true);
        return this.list(wrapper);
    }

    public RoomStateRecord getCurrentReserveStatus(Long roomHouseId) {
        LambdaQueryWrapper<RoomStateRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomStateRecord::getRoomId, roomHouseId);
        wrapper.eq(RoomStateRecord::getState, RentStatusEnum.SCHEDULED.getValue());
        wrapper.eq(RoomStateRecord::getIsCurrentState, true);
        return this.getOne(wrapper);
    }
}
